'use strict';
const moment = require('moment');
module.exports = app => {
  const { STRING, INTEGER, DATE } = app.Sequelize;
  const Contact = app.model.define('contact', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    name: {
      type: STRING,
    },
    sex: {
      type: INTEGER,
    },
    job: {
      type: STRING,
    },
    phone: {
      type: STRING,
    },
    telephone: {
      type: STRING,
    },
    email: {
      type: STRING,
    },
    created_at: {
      type: DATE,
      get() {
        return moment(this.getDataValue('created_at')).format(
          'YYYY-MM-DD HH:mm:ss'
        );
      },
    },
    updated_at: {
      type: DATE,
      get() {
        return moment(this.getDataValue('updated_at')).format(
          'YYYY-MM-DD HH:mm:ss'
        );
      },
    },
  });
  Contact.associate = () => {
    Contact.belongsToMany(app.model.Customer.Customer, {
      through: app.model.Customer.ContactCustomer,
      foreignKey: 'contact',
      otherKey: 'customer',
      as: 'Contacts',
    });
  };
  return Contact;
};
